package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.agreement.AgreementQueryVo;
import com.atguigu.lease.web.admin.vo.agreement.AgreementVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {
     @Autowired
     private LeaseAgreementMapper leaseAgreementMapper;
     @Autowired
     private ApartmentInfoMapper apartmentInfoMapper;
     @Autowired
     private RoomInfoMapper roomInfoMapper;
     @Autowired
     private PaymentTypeMapper paymentTypeMapper;
     @Autowired
     private LeaseTermMapper leaseTermMapper;
    @Override
    public AgreementVo customAgreement(Long id) {
        //租约
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);
        if (leaseAgreement==null){
            return null;
        }
        //公寓
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(leaseAgreement.getApartmentId());
        //房间
        RoomInfo roomInfo = roomInfoMapper.selectById(leaseAgreement.getRoomId());
        //支付方式
        PaymentType paymentType = paymentTypeMapper.selectById(leaseAgreement.getPaymentTypeId());
        //租期
        LeaseTerm leaseTerm = leaseTermMapper.selectById(leaseAgreement.getLeaseTermId());

        AgreementVo agreementVo = new AgreementVo();
        agreementVo.setApartmentInfo(apartmentInfo);
        agreementVo.setRoomInfo(roomInfo);
        agreementVo.setPaymentType(paymentType);
        agreementVo.setLeaseTerm(leaseTerm);
        BeanUtils.copyProperties(leaseAgreement,agreementVo);
        return agreementVo;
    }

    @Override
    public void findPage(Page<AgreementVo> page, AgreementQueryVo queryVo) {
        leaseAgreementMapper.queryPage(page,queryVo);
    }

    @Override
    public void removeLease(Long id) {
        LambdaQueryWrapper<LeaseAgreement> leaseAgreementLambdaQueryWrapper = new LambdaQueryWrapper<>();
        leaseAgreementLambdaQueryWrapper.eq(LeaseAgreement::getId,id);
        leaseAgreementLambdaQueryWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING,LeaseStatus.RENEWING);
        long count = count(leaseAgreementLambdaQueryWrapper);
        if (count>0){
            throw new LeaseException(ResultCodeEnum.STATUS_ERROR);
        }
        removeById(id);
    }
}




